/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "nicklist.h"
#include <QtSql/QSqlDatabase>
#include <QVariant>
#include <QHttp>
#include <QUrl>
#include <QMessageBox>
#include <QByteArray>
#include <QList>

const NickList::Nick *NickList::operator[](const QString &nick) const
{
	if (nickList.contains(nick)) {
		return &(*nickList.find(nick));
	} else {
		return NULL;
	}
}

QList<QString> NickList::list() const
{
	return nickList.keys();
}

void NickList::loadFromUrl(const QString &url)
{
	QUrl uri(url);
	http.setHost(uri.host());
	getId = http.get(uri.path());
}

void NickList::saveToDb()
{
	QSqlQuery query;
	query.exec("DELETE FROM nick_list");
	
	for (QMap<QString, Nick>::const_iterator it = nickList.begin(); it != nickList.end(); ++it) {
		query.prepare("INSERT INTO nick_list (nick, g, d, a, l, i) VALUES (:nick, :g, :d, :a, :l, :i)");
		query.bindValue(":nick", it->nick);
		query.bindValue(":g", it->g);
		query.bindValue(":d", it->d);
		query.bindValue(":a", it->a);
		query.bindValue(":l", it->l);
		query.bindValue(":i", it->i);
		query.exec();
	}
}

void NickList::loadFromDb()
{
	nickList.clear();
	
	QSqlQuery query;
	query.exec("SELECT nick, g, d, a, l, i FROM nick_list");
	
	while (query.next()) {
		Nick nick;
		nick.nick = query.value(0).toString();
		nick.g = query.value(1).toString();
		nick.d = query.value(2).toString();
		nick.a = query.value(3).toString();
		nick.l = query.value(4).toString();
		nick.i = query.value(5).toString();
		
		nickList.insert(nick.nick, nick);
	}
}

void NickList::on_http_requestFinished(int id, bool error)
{
	if (id != getId) return;
	if (error) {
		QMessageBox::warning(
			0, tr("Update nick list"),
			tr("Spiral could not fetch nick list from the given location. Please check the configuration.\nError message: ") + http.errorString());
		return;
	}
	
	nickList.clear();
	QByteArray data = http.readAll();
	QList<QByteArray> lines = data.split('\n');
	
	for (QList<QByteArray>::iterator it = lines.begin(); it != lines.end(); ++it) {
		
		it->replace("\r", "");
		QList<QByteArray> cols = it->split('\t');
		
		if (cols.count() != 6) continue;	// stlpcov ma byt 6
		
		Nick nick;
		nick.nick = cols[0];
		nick.g = cols[1];
		nick.d = cols[2];
		nick.a = cols[3];
		nick.l = cols[4];
		nick.i = cols[5];
		
		nickList.insert(nick.nick, nick);
	}
	
	saveToDb();
	QMessageBox::information(0, tr("Update nick list"), tr("Update successful."));
}

NickList::NickList()
{
	loadFromDb();
	getId = -1;
	
	connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(on_http_requestFinished(int, bool)));
}


NickList::~NickList()
{
}


